package org.android.kumoh_ing;

import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public class GtestActivity extends Activity{
	TextView feedList;
    FeedListAdapter2 feedAdapter;
	FeedListView feedList2;
    FeedListAdapter2 feedAdapter2;
    String index = "";
    String s_name = "";
    String s_date = "";
    String s_msg = "";
    String s_add = "";
    private Button mBtnWrite;
	private EditText mInText;
    
	 public void onCreate(Bundle savedInstanceState) {
	       super.onCreate(savedInstanceState);
	       setContentView(R.layout.gtest);
	       index = GroupTextActivity.s_id;
	       s_name = GroupTextActivity.s_name; 
	       s_date = GroupTextActivity.s_date;
	       s_msg = GroupTextActivity.s_msg;
	       s_add = GroupTextActivity.s_add;
	       
	       feedList = (TextView)findViewById(R.id.feedList);
	       
	       feedList2 = (FeedListView) findViewById(R.id.feedList2);
	       feedAdapter2 = new FeedListAdapter2(this);
	       feedList2.setAdapter(feedAdapter2);
	       mBtnWrite = (Button) findViewById(R.id.button2);
	       mInText = (EditText) findViewById(R.id.editText1);
	       
	       get_reply(index);
	       
			mBtnWrite.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String statusText = mInText.getText().toString();
					if (statusText.length() < 1) {
						Toast.makeText(getApplicationContext(), "占쌉뤄옙 占싹쇽옙占쏙옙.",	1000).show();
						return;
					}
					index = GroupTextActivity.s_id;
					updateStatus(statusText, index);
					get_reply(index);
				}
			});

	        
	 }
	 private void updateStatus(String statusText, String str) {
			try {
				Bundle params = new Bundle();
				params.putString("message", statusText);
					facebookvalue.FacebookInstance.request(str + "/comments", params, "POST");
				

				mInText.setText("");
				Toast.makeText(getApplicationContext(), statusText + " 전송 완료", 1000)
						.show();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	 
	 private void get_reply(String str){
			String msg="";
			String message="";
			String curMessage = "";
			String curName = "";
			feedAdapter2.clear();
			
			FeedItem curItem = new FeedItem(s_name, s_date, s_msg, null,s_add);
			feedAdapter2.addItem(curItem);
			
			try {
	    		String responseStr = facebookvalue.FacebookInstance.request(str+"/comments");
	    		
	    		JSONObject resultObj = Util.parseJson(responseStr);
				JSONArray jArray = resultObj.getJSONArray("data");
				
				for(int i = 0; i < jArray.length(); i++) {
					
					JSONObject obj = jArray.getJSONObject(i);
					JSONObject fromObj = obj.getJSONObject("from");
					
					try {
						curMessage = obj.getString("message");
						curName = fromObj.getString("name");
						
						curMessage=curMessage.replaceAll("#%UserLocation=",""); 
						StringTokenizer st = new StringTokenizer(curMessage, "//");
						while (st.hasMoreTokens()) { // 3
							st.nextToken();
							curMessage = st.nextToken(); 
						}
								
							} catch(Exception ex) {
								ex.printStackTrace();
							}
							
							Log.d("reply", "reply : "+curName+"  : "+curMessage );
							msg +=curName+" : "+curMessage ;
							if(i!=jArray.length()-1)
								msg +="\n";
							
						}
						if(jArray.length()==0){
							msg="입력 하세요";
						}
						feedList.setText(msg);
						feedList.setTextColor(Color.BLACK);

			} catch(Exception ex) {
				ex.printStackTrace();
			} catch(FacebookError ext) {
				ext.printStackTrace();
			}
			
			
		}
	 
	
}

/*package org.android.kumoh_ing;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public class GtestActivity extends Activity {
    *//** Called when the activity is first created. *//*
	TextView txt;
	TextView txt1;
	TextView txt2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gtest);
        txt = (TextView)findViewById(R.id.text);
        String index = "";
        index = GroupTextActivity.temp_id;
        Log.i("  ", "     "+index);
        String tmpStr ="";
        tmpStr = get_reply(index);
        txt.setText(tmpStr);
    }
    
    private String get_reply(String str){
		String msg="";
    	try {
    		String responseStr = facebookvalue.FacebookInstance.request(str+"/comments");
    		
    		JSONObject resultObj = Util.parseJson(responseStr);
			JSONArray jArray = resultObj.getJSONArray("data");
			
			for(int i = 0; i < jArray.length(); i++) {
				
				JSONObject obj = jArray.getJSONObject(i);
				JSONObject fromObj = obj.getJSONObject("from");
				String curMessage = "";
				String curName ="";
				try {
					curMessage = obj.getString("message");
					curName = fromObj.getString("name");
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				
				Log.d("reply", "reply : "+curName+"  : "+curMessage);
				msg +=curName+" : "+curMessage;
				if(i!=jArray.length()-1)
					msg +="\n";
				
			}
			if(jArray.length()==0){
				msg="占쏙옙溝占�占쏙옙占쏙옙占�占쏙옙占쏙옙求占�;
			}

    	} catch(Exception ex) {
    		ex.printStackTrace();
    	} catch(FacebookError ext) {
			ext.printStackTrace();
		}
    	return msg;
    }
}*/